var express = require('express'),
    router = express.Router(),
    tool = require('../utils/common'),
    bookModel = require('../db/model/book');

router.post('/index', (req, res) => {
    try {
        let params = req.body, page = 1;
        'page' in params && (page = params.page);

        //查询
        let model = new bookModel(), limit = model.getLimit(page),
            where = {};
        model.list(where, limit).then(result => {
            res.json(tool.returnArray({
                'current_page': page,
                'last_page': page,
                'total': 16,
                'data': result,
            }, 1));
        }).catch(e => res.json(tool.errorHandle(e)));
    } catch (e) {
        res.json(tool.errorHandle(e));
    }
});

router.post('/read', (req, res) => {
    try {
        let params = req.body;
        if (!params || Object.keys(params).length) tool.throwError('参数错误');

        let model = new bookModel();
        model.list(params, 1).then(result => {
            res.json(tool.returnArray(result, 1));
        }).catch(e => res.json(tool.errorHandle(e)));
    } catch (e) {
        res.json(tool.errorHandle(e));
    }
});

module.exports = router;